<?php
$loader->requireOnce('includes/LabException.class.php');
$loader->requireOnce('includes/LabAdmin/LabAdminProcessor.class.php');


/**
 * Controler for lab management.
 * @package modules.dept_laboratory.controllers
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class C_LabAdmin extends Controller {
    private $logger ;
    private $labProcessor;

    function __construct($template_mod = "general") {
        parent::Controller();
        $this->setLayout('admin');
        $this->logger = LoggerManager::getLogger('module.dept_laboratory.controllers.C_LabAdmin');
        $this->template_mod = $template_mod;
        $this->view->path="lab_admin";
        $this->labProcessor = new LabAdminProcessor($this);

    }
    /**
     * Render lab admin page.
     * @return html
     */
    public function renderAdminPage() {
        $this->logger->info('Start renderAdminPage');
        $html = $this->view->render('lab_admin.html');
        $this->logger->info('Finish renderAdminPage');
        return $html;
    }

    public function actionLoadLabGroup() {
        $this->logger->info('Start actionLoadLabGroup');
        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');

        try {
            $this->assign('labGroups', $this->labProcessor->listOfLabGroup());
            $this->assign('selectedLabGroup', 0);
            $result['result'] = $this->view->render('lab_group.html');

        }  catch (Exception $ex) {
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $logger->error('actionLoadLabGroup fail. Error detail: '. $ex->getMessage());
        }

        $this->logger->info('Finish actionLoadLabGroup');
        return json_encode($result);
         
    }

    public function actionLoadLabService() {
        $this->logger->info('Start actionLoadLabService');
        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');

        try {
            $this->assign('labGroups', $this->labProcessor->listOfLabGroup());
            $this->assign('selectedLabGroup', 0);
            $this->assign('labServices', '');
            $result['result'] = $this->view->render('lab_service.html');

        }  catch (Exception $ex) {
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $logger->error('actionLoadLabService fail. Error detail: '. $ex->getMessage());
        }

        $this->logger->info('Finish actionLoadLabService');
        return json_encode($result);
         
    }

    public function actionLoadLabResultStructure() {
        $this->logger->info('Start actionLoadLabResultStructure');
        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');

        try {
            $this->assign('labGroups', $this->labProcessor->listOfLabGroup());
            $unit = Utils::getEnumArrayKey(EnumerationConstant::UNIT);
            $dataType = Utils::getEnumArrayKey(EnumerationConstant::LAB_DATATYPE);
            $this->assign('unit', $unit);
            $this->assign('dataType', $dataType);
            $result['result'] = $this->view->render('lab_result_structure.html');

        }  catch (EnumException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('create lab actionLoadLabResultStructure fail. Error detail: '. $ex->getMessage());
        } catch (Exception $ex) {
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $logger->error('actionLoadLabResultStructure fail. Error detail: '. $ex->getMessage());
        }

        $this->logger->info('Finish actionLoadLabResultStructure');
        return json_encode($result);
         
    }

    /**
     * List lab services of selected group.
     * @return Array(service_id,name,price,insurance_price,patient_price)
     */
    public function actionListLabServicesOfSelectedGroupAjax() {
        $this->logger->info('Start actionListLabServicesOfSelectedGroupAjax');
        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');
        $selectedGroupId = $_POST['labGroupId'];
        $result['result'] =  $this->labProcessor->listLabServiceOfSelectedGroup($selectedGroupId);
        $this->logger->info('Finish actionListLabServicesOfSelectedGroupAjax');
        return json_encode($result);

    }

    public function actionSaveLabGroup($labGroupId='', $labGroupText='') {
        $this->logger->info('Start actionSaveLabGroup');
        if (isset($_POST['labGroupId'])) {
            $labGroupId = $_POST['labGroupId'];
        }

        if (isset($_POST['labGroupText'])) {
            $labGroupText = $_POST['labGroupText'];
        }

        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');

        try {
            $result['result'] = $this->labProcessor->saveLabGroup($labGroupId, $labGroupText);
            if ($result['result'] == 0) {
                throw new LabException("save fail data[" . $labGroupId . ',' . $labGroupText . ']',
                LabErrorCode::SAVE_FAIL);
            }

        }
        catch (LabException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('actionSaveLabGroup fail. Error detail: '. $ex->getMessage());
        }
        catch (Exception $ex) {
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $logger->error('actionSaveLabGroup fail. Error detail: '. $ex->getMessage());
        }

        $this->logger->info('Finish actionSaveLabGroup');
        return json_encode($result);
    }

    public function actionSaveLabElement() {
        $this->logger->info('Start actionSaveLabElement');
        if (isset($_POST['labGroupId'])) {
            $labGroupId = $_POST['labGroupId'];
        }

        if (isset($_POST['labGroupText'])) {
            $labGroupText = $_POST['labGroupText'];
        }

        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');

        try {
            $result = $this->labProcessor->saveLabGroup($labGroupId, $labGroupText);
            if ($result == 0) {
                throw new LabException('save fail data[]',
                LabErrorCode::SAVE_FAIL);
            }

        }
        catch (LabException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('actionSaveLabElement fail. Error detail: '. $ex->getMessage());
        }
        catch (Exception $ex) {
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $logger->error('actionSaveLabElement fail. Error detail: '. $ex->getMessage());
        }

        $this->logger->info('Finish actionSaveLabElement');
        return json_encode($result);
    }

    public function actionUpdateLabServiceAjax() {
        $this->logger->info('Start actionUpdateLabServiceAjax');
        $serviceId = $_POST['serviceId'];
        $name = $_POST['serviceName'];
        $price = $_POST['price'];
        $disabled = $_POST['disabled'];

        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');

        try {
            $this->labProcessor->updateLabService($serviceId,$name, $price, $disabled);

        }
        catch (LabException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('actionUpdateLabServiceAjax fail. Error detail: '. $ex->getMessage());
        }
        catch (Exception $ex) {
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $logger->error('actionUpdateLabServiceAjax fail. Error detail: '. $ex->getMessage());
        }

        $this->logger->info('Finish actionUpdateLabServiceAjax');
        return json_encode($result);
    }

    public function actionAddLabServiceAjax() {
        $this->logger->info('Start actionAddLabServiceAjax');
        $labGroupId = $_POST['labGroupId'];
        $name = $_POST['serviceName'];
        $price = $_POST['price'];
        $disabled = $_POST['disabled'];

        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');

        try {
            $result['result'] = $this->labProcessor->addLabService($labGroupId,$name, $price, $disabled);
            if ($result['result'] == 0) {
                throw new LabException('save fail data[' . $labGroupId .
                "," . $name . "," . $price . "," . $disabled . "]",
                LabErrorCode::SAVE_FAIL);
            }

        }
        catch (LabException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('actionAddLabServiceAjax fail. Error detail: '. $ex->getMessage());
        }
        catch (Exception $ex) {
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $logger->error('actionAddLabServiceAjax fail. Error detail: '. $ex->getMessage());
        }

        $this->logger->info('Finish actionAddLabServiceAjax');
        return json_encode($result);
    }


    public function actionListLabResultOfSelectedServiceAjax() {
        $this->logger->info('Start actionListLabResultOfSelectedServiceAjax');
        $labServiceId = $_POST['labServiceId'];

        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');

        try {
            $result['result'] = $this->labProcessor->listLabResultOfSelectedService($labServiceId);

        }
        catch (LabException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('actionListLabResultOfSelectedServiceAjax fail. Error detail: '. $ex->getMessage());
        }
        catch (Exception $ex) {
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $logger->error('actionListLabResultOfSelectedServiceAjax fail. Error detail: '. $ex->getMessage());
        }

        $this->logger->info('Finish actionListLabResultOfSelectedServiceAjax');
        return json_encode($result);
    }
    public function actionUpdateLabResultAjax() {
        $this->logger->info('Start actionUpdateLabResultAjax');
        $labResultId = $_POST['resultId'];
        $resultName = $_POST['resultName'];
        $labResultNormalMale = $_POST['labResultNormalMale'];
        $labResultNormalFemale = $_POST['labResultNormalFemale'];
        $unit = $_POST['unit'];
        $dataType = $_POST['dataType'];

        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');

        try {
            $this->labProcessor->updateLabResult($labResultId, $resultName,
            $labResultNormalMale, $labResultNormalFemale, $unit, $dataType);
        }
        catch (LabException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('actionUpdateLabResultAjax fail. Error detail: '. $ex->getMessage());
        }
        catch (Exception $ex) {
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $logger->error('actionUpdateLabResultAjax fail. Error detail: '. $ex->getMessage());
        }

        $this->logger->info('Finish actionUpdateLabResultAjax');
        return json_encode($result);
    }
    
    public function actionAddLabResultAjax() {
        $this->logger->info('Start actionAddLabResultAjax');
        $labServiceId = $_POST['labServiceId'];
        $resultName = $_POST['resultName'];
        $labResultNormalMale = $_POST['labResultNormalMale'];
        $labResultNormalFemale = $_POST['labResultNormalFemale'];
        $unit = $_POST['unit'];
        $dataType = $_POST['dataType'];

        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');

        try {
            $result['result'] = $this->labProcessor->addLabResult($labServiceId, $resultName,
            $labResultNormalMale, $labResultNormalFemale, $unit, $dataType);
            if ($result['result'] == 0) {
                throw new LabException('save fail data[' . $labServiceId . "," 
                . $resultName . "," 
                . $labResultNormalMale . "," 
                . $labResultNormalFemale . ","
                . $unit . ","
                . $dataType . "]",
                LabErrorCode::SAVE_FAIL);
            }
        }
        catch (LabException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('actionAddLabResultAjax fail. Error detail: '. $ex->getMessage());
        }
        catch (Exception $ex) {
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $logger->error('actionAddLabResultAjax fail. Error detail: '. $ex->getMessage());
        }

        $this->logger->info('Finish actionAddLabResultAjax');
        return json_encode($result);
    }




}
?>